package org.example.ui.administrator;

import org.example.business.MilkManageBusiness;
import org.example.business.MilkTeaBusiness;
import org.example.business.UserManageBusiness;
import org.example.manage.MilkTeaManage;
import org.example.model.MilkTea;
import org.example.model.User;
import org.example.ui.PromptComboBox;
import org.example.ui.SearchField;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.*;

public class MilkTeaAdminPanel extends JPanel {
    private JLabel[] topLabel = new JLabel[8];
    private JButton addButton = new JButton("新增");
    private static JScrollPane scrollPane;
    private SpringLayout layout = new SpringLayout();
    private static SpringLayout teaLayout = new SpringLayout();
    private JCheckBox box;
    private static JPanel teaPanel = new JPanel();
    private SearchField search;
    private JComboBox<String> comboBox;
    private PromptComboBox promptComboBox = new PromptComboBox(0);
    private final String[] str = {"价格升序", "价格降序", "库存升序", "库存降序", "分类排序"};
    private JLabel label = new JLabel("排序列方式:");
    private String[] topText = {"    索引    ", "      奶茶名称       ",
            "        奶茶图片        ", "       奶茶类型      ", "        奶茶库存     ",
            "    奶茶价格   ", "  是否上架    ", "   操作    "};
    public MilkTeaAdminPanel() {
        setLayout(layout);
        add(addButton);
        JTable table = new JTable();
        comboBox = new JComboBox<>(str);
        TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
        search = new SearchField();
        label.setOpaque(true);
        label.setFont(new Font("", Font.BOLD, 17));
        search.setFont(new Font("", Font.BOLD, 15));
        box = (JCheckBox) renderer;
        Image image2 = new ImageIcon("data/background/bag_icon.jpg").getImage().
                getScaledInstance(10, 10, Image.SCALE_DEFAULT);
        box.setIcon(new ImageIcon(image2));
        box.setSelectedIcon(new ImageIcon(image2));
        add(box);
        teaPanel.setLayout(teaLayout);
        scrollPane = new JScrollPane(teaPanel);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        scrollPane.getVerticalScrollBar().setUnitIncrement(20);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        teaPanel.setBackground(null);
        teaPanel.setOpaque(false);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        add(comboBox);
        add(label);
        add(search);
        add(promptComboBox);
        add(scrollPane);
        layout.putConstraint(SpringLayout.WEST, search, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, search, 10, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, search,-250, SpringLayout.HORIZONTAL_CENTER,this);
        layout.putConstraint(SpringLayout.SOUTH, search, 50, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, promptComboBox, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, promptComboBox, 0, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.SOUTH, promptComboBox, 0, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.EAST, promptComboBox, 0, SpringLayout.EAST, search);
        layout.putConstraint(SpringLayout.WEST, box, 15, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, box, 60, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.NORTH, label, 20, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.EAST, label, -10, SpringLayout.WEST, comboBox);
        layout.putConstraint(SpringLayout.EAST, comboBox, -10, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.NORTH, comboBox, 20, SpringLayout.SOUTH, search);
        layout.putConstraint(SpringLayout.EAST, addButton, -10 , SpringLayout.WEST, label);
        layout.putConstraint(SpringLayout.NORTH, addButton, 20, SpringLayout.SOUTH, search);
        Font font = new Font("", Font.BOLD, 15);
        for (int i = 0; i < 8; i++) {
            topLabel[i] = new JLabel(topText[i], JLabel.CENTER);
            topLabel[i].setFont(font);
            topLabel[i].setOpaque(true);
            topLabel[i].setBackground(Color.WHITE);
            topLabel[i].setBorder(null);
            add(topLabel[i]);
            if (i == 0) {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 10, SpringLayout.EAST, box);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 10, SpringLayout.SOUTH, addButton);
            }
            else {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 0, SpringLayout.EAST, topLabel[i-1]);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 10, SpringLayout.SOUTH, addButton);
            }
            if (i == 7) {
                layout.putConstraint(SpringLayout.EAST, topLabel[i], -20, SpringLayout.EAST, this);
            }
        }
        setAllListener();
        MilkManageBusiness.teaLayout = teaLayout;
        MilkManageBusiness.teaPanel = teaPanel;
        MilkManageBusiness.scrollPane = scrollPane;
        MilkManageBusiness.setAdminItems(0);
        layout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, scrollPane, 10 ,SpringLayout.SOUTH, topLabel[0]);
        layout.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST , scrollPane, -20, SpringLayout.EAST, this);
        setVisible(true);
    }
    private void setAllListener() {
        addButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MilkTeaAdminItem.isSave) {
                    new MilkTeaManage().save(new MilkTea());
                    comboBox.setSelectedIndex(0);
                    MilkManageBusiness.setAdminItems(1);
                    MilkTeaBusiness.setMilkTeas(0);
                    MilkTeaBusiness.setKinds();
                }
                else {
                    JOptionPane.showMessageDialog(MilkTeaAdminPanel.this, "你编辑的内容未保存，请保存");
                }
            }
        });
        comboBox.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MilkTeaAdminItem.isSave) {
                    if (e.getModifiers() == 16)
                        MilkManageBusiness.setAdminItems(comboBox.getSelectedIndex() + 2);
                }
                else {
                    JOptionPane.showMessageDialog(MilkTeaAdminPanel.this, "你编辑的内容未保存，请保存");
                }
            }
        });
        search.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) {
                    promptComboBox.update(search.getText());
                    promptComboBox.hidePopup();
                    promptComboBox.showPopup();
                }
                if (search.getText().isEmpty()) {
                    MilkManageBusiness.setAdminItems(0);
                    comboBox.setVisible(true);
                    addButton.setVisible(true);
                    label.setVisible(true);
                    search.requestFocus();
                }
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    teaPanel.removeAll();
                    boolean b = true;
                    int num = 1;
                    MilkTeaAdminItem teaAdminItem = null;
                    for (MilkTea milkTea : MilkManageBusiness.milkTeas) {
                        if (milkTea.milkName.contains(search.getText())) {
                            MilkTeaAdminItem item = new MilkTeaAdminItem(milkTea, num);
                            teaPanel.add(item);
                            if (b) {
                                b = false;
                                teaLayout.putConstraint(SpringLayout.NORTH, item, 0, SpringLayout.NORTH, teaPanel);
                                teaLayout.putConstraint(SpringLayout.SOUTH, item, 100, SpringLayout.NORTH, teaPanel);
                                teaAdminItem = item;
                            } else {
                                teaLayout.putConstraint(SpringLayout.NORTH, item, 1, SpringLayout.SOUTH, teaAdminItem);
                                teaLayout.putConstraint(SpringLayout.SOUTH, item, 100, SpringLayout.SOUTH, teaAdminItem);
                                teaAdminItem = item;
                            }
                            num ++;
                            teaLayout.putConstraint(SpringLayout.WEST, item, 0, SpringLayout.WEST, teaPanel);
                            teaLayout.putConstraint(SpringLayout.EAST, item, 0, SpringLayout.EAST, teaPanel);
                        }
                    }
                    comboBox.setVisible(false);
                    addButton.setVisible(false);
                    label.setVisible(false);
                    teaPanel.setVisible(false);
                    teaPanel.setVisible(true);
                    search.requestFocus();
                }
            }
        });
        promptComboBox.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getModifiers() == 16)
                    search.setText((String) promptComboBox.getSelectedItem());
            }
        });
    }
}
